#pragma once

#include <QThread>
#include <QFileSystemWatcher>
#include <QSqlDatabase>
#include <QList>
#include <QByteArray>
#include <QSettings>
#include <QTimer>
#include <memory>

typedef QList<QByteArray> QByteArrayList;

class Worker : public QThread
{
    Q_OBJECT 

public:
    Worker(const QSettings& ini);

private:
	virtual void run();

private slots:
	void Diff(const QString& path = QString());

private:
    QByteArrayList GetDiff(const QByteArrayList& data);
	
private:
    QByteArrayList GetData();
    void SaveData(const QByteArrayList& data);

private:
    bool LoadCache();
    bool SaveCache();
    bool UpdateCache(const QByteArrayList& data);

private:
    bool MakeConnection();
    bool MakePath(const QString& path);
    bool CheckSource();

private:
    typedef std::tr1::shared_ptr<QFileSystemWatcher> FileSystemWatcherPtr;
	FileSystemWatcherPtr _watcher;

private:
	QString         _table;
	QString         _resultPath;
    QString         _cachePath;
    QString         _cacheFile;
    QString         _dsn;
    QList<unsigned> _missedLines;

private:
    QByteArrayList  _cache;

private:
    QSqlDatabase    _db;

private:
    QTimer          _timer;
};

typedef std::tr1::shared_ptr<Worker> WorkerPtr;
